package com.kele.controller;

import com.kele.domain.User;
import com.kele.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/**
 * @author 赵奥林
 * @date 2021年05月10日 13:05
 * @Description  用户模块：
 *                    1.用户的基本操作：登陆注册、修改密码、修改个人信息
 */
@Controller
@RequestMapping(value = "/users")
public class UsersController {

    @Resource
    private UserService userService;

    //一、用户模块
    //用户登录
    @RequestMapping(value = "/loginUser",method = RequestMethod.POST)
    public String loginAccount(String account, String password,
                               HttpServletRequest request,
                               HttpServletResponse response) throws IOException {
        User user = userService.loginUser(account, password);
        if (user!=null){
            request.getSession().setAttribute("user",user);
            return "/index"; //登陆成功
        }else {
            return "/static_resources/templates/login"; //登陆失败
        }
    }

    //用户注册
    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    public String insertUser( String account,
                              String password,
                              String repassword){
        if (password.equals(repassword)) {
            User user=new User();
            user.setAccount(account);
            user.setPassword(password);
            if (userService.insertUser(user)>0){
                return "/static_resources/templates/login";//注册成功,跳转到登陆页面
            }else{
                return "/login";//用户名已存在
            }
        }else {
            return "/login";//输入密码不一致
        }

    }

    //用户退出登录
    @RequestMapping(value = "outLogin",method = RequestMethod.POST)
    public String outLogin(HttpServletRequest request){
        //注销当前session
        request.getSession().removeAttribute("user");//移除user这个session
        return "index";
    }

    //用户修改密码(使用旧密码验证)
    @RequestMapping(value = "/updateUserPassword",method = RequestMethod.POST)
    public String updateUserPassword(int id,
                                     String oldPassword,
                                     String newPassword,
                                     String rePassword){
        if (newPassword.equals(rePassword)){
            User user = userService.queryUserById(id);
            if (oldPassword.equals(user.getPassword())){
                user.setPassword(newPassword);
                return "1";//修改成功
            }else {
                return "2";//修改失败
            }
        }else {
            return "3";//两次密码不一致
        }
    }

    //跳转到个人信息界面
    @RequestMapping(value = "/toPeople")
    public String toPeople(){
        return "static_resources/templates/peopleCenter";
    }


    //用户修改个人信息
    @RequestMapping(value = "/updateAccount",method = RequestMethod.PUT)
    public String updateAccount(User user){
        if (userService.updateUser(user)>0){
            return "1";//修改成功
        }else{
            return "2";//修改失败
        }
    }

    //用户注销账户（删除账号）
    @RequestMapping(value = "/deleteAccount",method = RequestMethod.GET)
    public String deleteAccount(){
        userService.deleteAccount();
        return "123";
    }


//用户VIP （权限的判定）
    /**
     *  用于判断是否为VIP，
     * @param request 通过请求获取session
     * @return 返回一个只用于判断
     *  0 为普通用户
     *  1~3为VIP用户
     *  4 ~∞ 为Manager  （管理员）
     */
    @RequestMapping(value = "/isManager" )
    public int isManagerHelper(HttpServletRequest request){
        User user= (User) request.getSession().getAttribute("account");
        return user.getId();
    }


}
